package tree.递归;

import linkedList.合并K个升序链表23;
import po.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 最大二叉树654.java
 * @Description https://leetcode-cn.com/problems/maximum-binary-tree/
 * @createTime 2021年06月29日 19:39:00
 */
public class 最大二叉树654 {

    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return buildTree(nums, 0, nums.length);
    }

    private TreeNode buildTree(int[] nums, int start, int end) {
        //如果开始和结尾相等表示结束
        if (end <= start) {
            return null;
        }
        //寻找区间内的最大值
        int index = start;
        for (int i = start; i < end; i++) {
            if (nums[index] < nums[i]){
                index = i;
            }
        }
        TreeNode node = new TreeNode(nums[index]);
        node.left = buildTree(nums, start, index );
        node.right = buildTree(nums, index + 1, end);
        return node;
    }
}
